<?php

class SiteController extends Controller
{
	public $menu = array();
	
	public $mainMenu = array();
	
	public function __construct($id, $module=null) {
		parent::__construct($id, $module);
		
		$this->mainMenu = array(
			array('label'=>'Home', 'url'=>array('/site/index')),
			array('label'=>'Websites', 'url' => array('/site/websites')),
			array('label'=>'FAQs', 'url'=>array('/site/faqs')),
			array('label'=>'Contact Us', 'url'=>array('/site/contact')),
			array('label'=>'Sign in / up', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
			array('label'=>'My Account', 'url'=>array('/owner/account/overview'), 'visible'=>!Yii::app()->user->isGuest)
		);
		
		$this->menu =  array(
			array('label' => 'Websites', 'url' => array('site/websites')),
			array('label' => 'FAQs', 'url' => array('site/FAQs')),
			array('label' => 'Terms', 'url' => array('site/terms')),
			array('label' => 'Tips', 'url' => array('site/tips')),
			array('label' => 'Privacy Policy', 'url' => array('site/privacyPolicy')),
			array('label' => 'Contact Us', 'url' => array('site/contact')),
		);
	}
	
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$this->render('index');
	}
	
public function actionWebsites() {
		$this->layout='//layouts/column2';
		$this->render('websites', array('block_title' => 'Websites'));
	}
	
	public function actionFaqs() {
		$this->layout='//layouts/column2';
		$this->render('faqs', array('block_title' => 'FAQs'));
	}
	
	public function actionTerms() {
		$this->layout = '//layouts/column2';
		$this->render('terms', array('block_title' => 'Terms'));
	}
	
	public function actionTips() {
		$this->layout = '//layouts/column2';
		$this->render('tips', array('block_title' => 'Tips'));
	}
	
	public function actionPrivacyPolicy() {
		$this->layout = '//layouts/column2';
		$this->render('privacyPolicy', array('block_title' => 'Privacy Policy'));
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
	    if($error=Yii::app()->errorHandler->error)
	    {
	    	if(Yii::app()->request->isAjaxRequest)
	    		echo $error['message'];
	    	else
	        	$this->render('error', $error);
	    }
	}

	/**
	 * Displays the contact page
	 */
	public function actionContact()
	{
		$model=new ContactForm;
		if(isset($_POST['ContactForm']))
		{
			$model->attributes=$_POST['ContactForm'];
			if($model->validate())
			{
				$headers="From: {$model->email}\r\nReply-To: {$model->email}";
				mail(Yii::app()->params['adminEmail'],$model->subject,$model->body,$headers);
				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
				$this->refresh();
			}
		}
		$this->render('contact',array('model'=>$model));
	}

	/**
	 * Displays the login page
	 */
	public function actionLogin()
	{
		$loginForm=new LoginForm;

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($loginForm);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$loginForm->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if($loginForm->validate() && $loginForm->login()) {
				//$this->redirect(Yii::app()->user->returnUrl);
				$this->redirect(array('owner/account/overview'));
			}
		}
		
		$owner = new Owner();
		if(isset($_POST['ajax']) && $_POST['ajax']==='owner-form')
		{
			echo CActiveForm::validate($owner);
			Yii::app()->end();
		}
		
		if(isset($_POST['Owner']))
		{
			$owner->attributes=$_POST['Owner'];
			$owner->usertype = 2;
			$owner->ip = '192.168.8.63';
			$owner->remark = '0';
			$owner->request_pay_date = 0;
			$owner->request_pay_detail = '0';
			$owner->payableto = 0;
			if($owner->save()) {
				$owner->loginid = 'CP'. $owner->owner_id;
				$owner->save();
				$loginForm->username = $owner->loginid;
				$loginForm->password = $owner->passwd;
				$loginForm->login();
				$this->redirect(array('site/registerSuccess','id'=>$owner->owner_id));
			}
		}
		// display the login form
		$this->render('sign', array('loginForm' => $loginForm, 'owner' => $owner));
	}
	
	public function actionRegisterSuccess($id) {
		$owner = Owner::model()->findByPk($id);
		$this->render('registerSuccess', array('owner' => $owner));
	}

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
}